// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "front/type_conv.h"

using namespace std;

namespace ppspeech {
// wstring to string
string wstring2utf8string(const wstring& fpInWstring)
{
	static wstring_convert<codecvt_utf8<wchar_t>> tConvert;
	return tConvert.to_bytes(fpInWstring);
}

// string to wstring
wstring utf8string2wstring(const string& fpInString)
{
	static wstring_convert<codecvt_utf8<wchar_t>> tConvert;
	return tConvert.from_bytes(fpInString);
}

vector<string> SplitUtf8(const string s)
{
    vector<string> split_char;
    for (size_t i = 0; i < s.length();)
    {
        int cplen = 1; // record bytes of char
        if ((s[i] & 0xf8) == 0xf0)
        {
            cplen = 4;
        }
        else if ((s[i] & 0xf0) == 0xe0)
        {
            cplen = 3;
        }
        else if ((s[i] & 0xe0) == 0xc0)
        {
            cplen = 2;
        }

        if ((i + cplen) > s.length())
        {
            cplen = 1;
        }
        split_char.push_back(s.substr(i, cplen));
        i += cplen;
    }
    return split_char;
}

vector<string> StrSplit(const string& fpInString, const string& fpInPattern, size_t fInMaxsplit)
{
	vector<string> tResult;
	size_t tStartPos = 0;
	size_t tEndPos = 0;
	string tSubstr = "";
	while(tStartPos < fpInString.size())
	{
		tEndPos = fpInString.find_first_of(fpInPattern, tStartPos);
		if(string::npos == tEndPos || tResult.size() >= fInMaxsplit) {
			tSubstr = fpInString.substr(tStartPos);
			tResult.push_back(tSubstr);
			return tResult;
		}
		tSubstr = fpInString.substr(tStartPos, tEndPos - tStartPos);
		tResult.push_back(tSubstr);
		tStartPos = tEndPos + 1;
	}
	return tResult;
}

void StrReplace(string& fpInString, const string fInStrPattern, const string fInStrReplace)
{
	size_t tPosition = 0;
	while( (tPosition = fpInString.find(fInStrPattern)) != string::npos )
	{
		fpInString.replace(tPosition, fInStrPattern.size(), fInStrReplace);
	}
}

void WstrReplace(wstring& fpInWstring, const string fInStrPattern, const string fInStrReplace)
{
	size_t tPosition = 0;
	const wstring tWstrPattern = utf8string2wstring(fInStrPattern);
	const wstring tWstrReplace = utf8string2wstring(fInStrReplace);
	while( (tPosition = fpInWstring.find(tWstrPattern)) != string::npos )
	{
		fpInWstring.replace(tPosition, tWstrPattern.size(), tWstrReplace);
	}
}

vector<string> GetFolderFilenames(const string fInFolderPath)
{
	vector<string> tFilepath;
	DIR* tpDirp = NULL;
	struct dirent* tpDirEntry = NULL;
	char namebuf[100] = {0};
	if( (tpDirp = opendir(fInFolderPath.c_str())) == NULL ){
		cout << "Opendir fail! " << fInFolderPath << endl;
		return tFilepath;
	}
	while((tpDirEntry = readdir(tpDirp)) != NULL){
		tFilepath.push_back( fInFolderPath + tpDirEntry->d_name );
	}
	closedir(tpDirp);
	return tFilepath;
}

void DeleteVectorFiles(const vector<string> fInFilesVec, const string fInEndwith)
{
	for(string tPath: fInFilesVec) {
		if(tPath.find(fInEndwith) != string::npos) {
			if( remove(tPath.c_str()) == 0 ) {
				cout << "删除缓存文件：" << tPath << endl;
			}
		}
	}
}

#include <sys/time.h>

double getTimeNow()
{
	struct timeval time;
	if (gettimeofday(&time, NULL))
	{
		return 0;
	}
	return (double)(time.tv_sec) + (double)(time.tv_usec) * 0.000001;
}

}  // namespace ppspeech
